<?php
/**
 * 云信语音接口
 */
class Voice {
    private $appId;
    private $Token;
    private $baseUrl;
    private $middleNumber;

    public function __construct() {
        $this->appId='736970';
        $this->Token='50269d5858a04532bf5a869f81ba7aa2';
        $this->baseUrl='https://101.37.133.245:11008/voice/1.0.0/';
        $this->middleNumber=['01086398150','01086399655','01086462216'];
    }

    /**
     * 随机获取小号
     * @return mixed
     */
    function getMiddleNumber(){
        $middleNumber =$this->middleNumber;
        return $middleNumber[rand(0,count($middleNumber)-1)];
    }

    /**
     * AXB模式绑定
     * @param $bindNumberA
     * @param $bindNumberB
     * @return mixed
     */
    public function middleNumberAXB($bindNumberA,$bindNumberB){
        $url=$this->baseUrl.'middleNumberAXB';
//        $params['middleNumber']=$this->getMiddleNumber();
        $params['bindNumberA']=$bindNumberA;
        $params['bindNumberB']=$bindNumberB;
        $params['callRec']='1';
        $params['callbackUrl']='http://app.tytpay.top/api/task/receiveVoice';
        $response=$this->postRequest($url,$params);
        $result = json_decode($response,true);
        return $result;
    }

    /**
     * 小号解绑
     * @param $bindNumberA
     * @param $bindNumberB
     * @param $middleNumber
     * @return mixed
     */
    public function middleNumberUnbind($bindNumberA,$bindNumberB,$middleNumber){
        $url=$this->baseUrl.'middleNumberUnbind';
        $params['middleNumber']=$middleNumber;
        $params['bindNumberA']=$bindNumberA;
        $params['bindNumberB']=$bindNumberB;
        $response=$this->postRequest($url,$params);
        $result = json_decode($response,true);
        return $result;
    }

    /**
     * 语音通知
     * @param $mobile
     * @return mixed
     */
    public function notify($mobile,$customerData=''){
        $url=$this->baseUrl.'notify';
        $params['templateId']='401365';
        $params['templateArgs']='{}';
        $params['calleeNumber']=$mobile;
        $params['replayTimes']='3';
        $params['customerData']=$customerData;
        $params['callbackUrl']='http://app.tytpay.top/api/task/receiveRecord';
        $response=$this->postRequest1($url,$params);
        $result = json_decode($response,true);
        return $result;
    }

    public function notify1($mobile,$customerData=''){
        $url=$this->baseUrl.'notify';
        $params['templateId']='401714';
        $params['templateArgs']='{}';
        $params['calleeNumber']=$mobile;
        $params['replayTimes']='3';
        $params['customerData']=$customerData;
        $params['callbackUrl']='http://app.tytpay.top/api/task/receiveRecord1';
        $response=$this->postRequest1($url,$params);
        $result = json_decode($response,true);
        return $result;
    }


    function postRequest($url,$params){
        $timestamp = time()*1000;
        $Authorization =base64_encode($this->appId.':'.$timestamp);
        $url .="/".$this->appId;
        $url .="/".md5($this->appId.$this->Token.$timestamp);
        $fields = json_encode($params);
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER=>false,
            CURLOPT_SSL_VERIFYHOST=>false,
            CURLOPT_POSTFIELDS =>$fields,
            CURLOPT_HTTPHEADER => array(
                'Authorization: '.$Authorization,
                'Content-Type: application/json'
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    function postRequest1($url,$params){
        $timestamp = time()*1000;
        $Authorization =base64_encode($this->appId.':'.$timestamp);
        $url .="/".$this->appId;
        $url .="/".md5($this->appId.$this->Token.$timestamp);
        //$fields = json_encode($params);
        $fields = '{
	"templateId": "'.$params['templateId'].'",
	"templateArgs": {},
	"calleeNumber": "'.$params['calleeNumber'].'",
	"customerData": "'.$params['customerData'].'",
	"callbackUrl": "'.$params['callbackUrl'].'"
}';
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER=>false,
            CURLOPT_SSL_VERIFYHOST=>false,
            CURLOPT_POSTFIELDS =>$fields,
            CURLOPT_HTTPHEADER => array(
                'Authorization: '.$Authorization,
                'Content-Type: application/json'
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

}